package com.zhouqinuan.spring.cloud.develop.framework.base.config.log;


import javax.servlet.Filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.zhouqinuan.spring.cloud.develop.framework.base.log.LogTraceFilter;

@Configuration
@ConditionalOnClass({Filter.class})
@EnableConfigurationProperties(LogProperties.class)
@ConditionalOnProperty(prefix = "log", name = "trace", havingValue = "enable", matchIfMissing = true)
public class LogConfiguration {

    @Autowired
    private LogProperties logProperties;

    @Bean
    public FilterRegistrationBean<LogTraceFilter> logTraceFilterFilterRegistration() {
        FilterRegistrationBean<LogTraceFilter> registration = new FilterRegistrationBean<LogTraceFilter>();
        registration.setFilter(logTraceFilter());
        registration.addUrlPatterns("/*");
        registration.setName("logTraceFilter");
        
        return registration;
    }

    @Bean
    public LogTraceFilter logTraceFilter() {
        LogTraceFilter logTraceFilter = new LogTraceFilter();
        logTraceFilter.setLogProperties(logProperties);

        return logTraceFilter;
    }
}
